//	Copyright (c) 2008, x-port.net ltd.
//
//	All rights reserved.
//
//	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
//	    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//	    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//	    * Neither the name of the x-port.net ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
//	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//	A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
//	CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
//	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
//	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//   Purpose: Implementation of CFPChecker.
//   $Id$
#include "stdafx.h"
#include "FPChecker.h"
#include "XForms.h"
#include "XForms_i.c"
#include <new>
#include <atlstr.h>
#include <Common/Filesystem/msw/FileSystemUtils.h>
#include <LibXPort/Types.h>

bool CFPChecker::sm_bInstalled = false;
CComBSTR CFPChecker::sm_sVersion;
WORD CFPChecker::sm_nVersion = 0;
WORD CFPChecker::sm_nMajorRev = 0;
WORD CFPChecker::sm_nMinorRev = 0;
WORD CFPChecker::sm_nBuild = 0;
bool CFPChecker::sm_bIsInstalledFlagSet = false;
bool CFPChecker::sm_bIsVersionStringSet = false;
bool CFPChecker::sm_bAreVersionNumbersSet = false;

const CComVariant cg_vFPVersion = L"FormsPlayerVersion";
const LPCTSTR cg_sFPRegKeyFormat = _T("CLSID\\%s");
const LPCTSTR cg_sVersionFormat = _T("%d.%d.%d.%d");
const LPCTSTR cg_sFPDLL = _T("\\XFormsProcessor.dll");
const LPTSTR cg_sVersionQuery = _T("\\");

CFPChecker::CFPChecker()
{
}

CFPChecker::~CFPChecker()
{
}

STDMETHODIMP CFPChecker::get_isInstalled(VARIANT_BOOL *pbIsInstalled)
{
	HRESULT hr = S_OK;

	if(pbIsInstalled)
	{
		if(!sm_bIsInstalledFlagSet)
		{
			//CComPtr<IFactory> pFPFactory;
			//pFPFactory.CoCreateInstance(CLSID_Factory);
			//sm_bInstalled = !!pFPFactory;
			//sm_bIsInstalledFlagSet = true;

			LPOLESTR sFPCLSID;
			hr = StringFromCLSID(CLSID_Factory, &sFPCLSID);
			if(SUCCEEDED(hr))
			{
				CString sFPRegKey;
				sFPRegKey.Format(cg_sFPRegKeyFormat, sFPCLSID);

				HKEY hKeyFP = 0;
				if(RegOpenKeyEx(HKEY_CLASSES_ROOT, sFPRegKey, 0, KEY_QUERY_VALUE, &hKeyFP) == ERROR_SUCCESS)
				{
					sm_bInstalled = true;
					RegCloseKey(hKeyFP);
				}

				sm_bIsInstalledFlagSet = true;

				CoTaskMemFree(sFPCLSID);
			}
		}

		*pbIsInstalled = sm_bInstalled ? VARIANT_TRUE : VARIANT_FALSE;
	}
	else
		hr = E_POINTER;

	return hr;
}

STDMETHODIMP CFPChecker::get_versionString(BSTR *psVersionString)
{
	HRESULT hr = S_OK;

	if(psVersionString)
	{
		if(!sm_bIsVersionStringSet)
		{
			//CComPtr<IXFormsXPathFunctions> pFPFunctions;
			//hr = pFPFunctions.CoCreateInstance(CLSID_XFormsXPathFunctions);
			//if(SUCCEEDED(hr))
			//{
			//	pFPFunctions->property(cg_vFPVersion, &sm_sVersion);
			//	sm_bIsVersionStringSet = true;
			//}

			hr = FetchVersion();
			if(SUCCEEDED(hr))
			{
				CString sVersion;
				sVersion.Format(cg_sVersionFormat, sm_nVersion, sm_nMajorRev, sm_nMinorRev, sm_nBuild);
				sm_sVersion.Attach(sVersion.AllocSysString());
				sm_bIsVersionStringSet = true;
			}
		}

		if(sm_sVersion.Length() > 0)
			hr = sm_sVersion.CopyTo(psVersionString);
	}
	else
		hr = E_POINTER;

	return hr;
}

STDMETHODIMP CFPChecker::get_versionNum(long *pnVersionNum)
{
	HRESULT hr = S_OK;

	if(pnVersionNum)
	{
		hr = FetchVersion();
		if(SUCCEEDED(hr))
			*pnVersionNum = sm_nVersion;
	}
	else
		hr = E_POINTER;

	return hr;
}

STDMETHODIMP CFPChecker::get_majorRev(long *pnMajorRev)
{
	HRESULT hr = S_OK;

	if(pnMajorRev)
	{
		hr = FetchVersion();
		if(SUCCEEDED(hr))
			*pnMajorRev = sm_nMajorRev;
	}
	else
		hr = E_POINTER;

	return hr;
}

STDMETHODIMP CFPChecker::get_minorRev(long *pnMinorRev)
{
	HRESULT hr = S_OK;

	if(pnMinorRev)
	{
		hr = FetchVersion();
		if(SUCCEEDED(hr))
			*pnMinorRev = sm_nMinorRev;
	}
	else
		hr = E_POINTER;

	return hr;
}

STDMETHODIMP CFPChecker::get_buildNum(long *pnBuildNum)
{
	HRESULT hr = S_OK;

	if(pnBuildNum)
	{
		hr = FetchVersion();
		if(SUCCEEDED(hr))
			*pnBuildNum = sm_nBuild;
	}
	else
		hr = E_POINTER;

	return hr;
}

HRESULT CFPChecker::FinalConstruct()
{
	return S_OK;
}

void CFPChecker::FinalRelease() 
{
}

HRESULT CFPChecker::FetchVersion(void)
{
	HRESULT hr = S_OK;

	try
	{
		if(!sm_bAreVersionNumbersSet)
		{
			CString sFP;
			hr = xport::GetHomeDirectory(_AtlBaseModule.m_hInst, sFP);
			if(SUCCEEDED(hr))
			{
				sFP += cg_sFPDLL;

				DWORD dwVersionInfoHandle = 0;
				DWORD dwSize = GetFileVersionInfoSize((LPTSTR)(LPCTSTR)sFP, &dwVersionInfoHandle);
				if(dwSize != 0)
				{
					LibXPort::byte_t *pVersionInfo = new LibXPort::byte_t[dwSize];

					if(GetFileVersionInfo((LPTSTR)(LPCTSTR)sFP, dwVersionInfoHandle, dwSize, pVersionInfo))
					{
						VS_FIXEDFILEINFO *pFixedInfo = 0;
						UINT nSize = 0;
						if(VerQueryValue(pVersionInfo, cg_sVersionQuery, reinterpret_cast<LPVOID *>(&pFixedInfo), &nSize) && pFixedInfo && nSize == sizeof(VS_FIXEDFILEINFO))
						{
#pragma warning(disable:4244)
							sm_nVersion = pFixedInfo->dwFileVersionMS >> 16;
							sm_nMajorRev = pFixedInfo->dwFileVersionMS;
							sm_nMinorRev = pFixedInfo->dwFileVersionLS >> 16;
							sm_nBuild = pFixedInfo->dwFileVersionLS;
#pragma warning(default:4244)
							sm_bAreVersionNumbersSet = true;
						}
						else
							hr = E_UNEXPECTED;
					}
					else
						hr = E_UNEXPECTED;

					delete[] pVersionInfo;
				}
				else
					hr = E_FAIL;
			}
		}
		else
			hr = S_FALSE;
	}
	catch(std::bad_alloc &)
	{
		hr = E_OUTOFMEMORY;
	}
	catch(...)
	{
		hr = E_UNEXPECTED;
	}

	return hr;
}
